package com.client.qiuqiu.adapters;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.client.qiuqiu.Friend;
import com.client.qiuqiu.listeners.Userlistener;
import com.client.qiuqiu.R;
import com.client.qiuqiu.databinding.ItemContaninerFriendBinding;

import java.util.List;

public class FriendsAdapter extends RecyclerView.Adapter<FriendsAdapter.FriendViewHolder>{

    private final List<Friend> friends;
    private final Userlistener userlistener;
    public FriendsAdapter(List<Friend> friends, Userlistener userlistener) {
        this.friends = friends;
        this.userlistener = userlistener;
    }

    @NonNull
    @Override
    public FriendViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemContaninerFriendBinding itemContaninerFriendBinding = ItemContaninerFriendBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent,
                false
        );
        return new FriendViewHolder(itemContaninerFriendBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull FriendViewHolder holder, int position) {
        holder.setFriendData(friends.get(position));
    }

    @Override
    public int getItemCount() {
        return friends.size();
    }

    class FriendViewHolder extends RecyclerView.ViewHolder{
        ItemContaninerFriendBinding binding;

        FriendViewHolder(ItemContaninerFriendBinding itemContaninerFriendBinding){
            super(itemContaninerFriendBinding.getRoot());
            binding = itemContaninerFriendBinding;
        }

        void setFriendData(Friend friend){
            binding.textName.setText(friend.name);
            binding.textID.setText(friend.id);
            if(friend.state.equals("1")){
                binding.getRoot().setOnClickListener(view -> userlistener.onUserClicked(friend));
            }else{
                binding.imageProfile.setBackgroundResource(R.mipmap.offline);
            }

        }
    }
}
